$(function() {

	initPageInfo();

	$("#submitBtn").click(function() {
		saveOrUpdateInspectInfo();

	});

});

$("#goHome").click(
		function() {

			$("#goHome").attr(
					"href",
					rootPath + "/ui/bussinessmain?device=mobile&&identify="
							+ $("#identify").val());
		});
$('#fileclick').on('click', function() {
	return $("#file_upload").click();
});
$("#file_upload")
		.change(
				function() {
					var $file = $(this);
					var fileObj = $file[0];
					var windowURL = window.URL || window.webkitURL;
					var dataURL;
					var $img = $("#avatart");

					if (fileObj && fileObj.files && fileObj.files[0]) {
						dataURL = windowURL.createObjectURL(fileObj.files[0]);
						$img.attr('src', dataURL);
					} else {
						dataURL = $file.val();
						var imgObj = document.getElementById("preview");
						// 两个坑:
						// 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
						// 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
						imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
						imgObj.filters
								.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

					}
				});
function initPageInfo() {
	$.ajax({
		url : window.rootPath + "/ui/bussinessInspection/findOptsByGroup",
		dataType : 'JSON',
		type : 'get',
		success : function(result) {

			if (result.code == 1) {

				var environment = result.data.environment;
				var cityOrder = result.data.cityOrder;
				var greenCondition = result.data.greenCondition;
				var noneOpt = result.data.noneOpt;
				
				var contenthtml = '<optgroup label="无">';
				for (var i = 0; i < noneOpt.length; i++) {
					contenthtml += '<option selected="selected" value="' + noneOpt[i].id + '">'
							+ noneOpt[i].title + '</option>';

				}

				contenthtml += '<optgroup label="环境卫生">';
				for (var i = 0; i < environment.length; i++) {
					contenthtml += '<option value="' + environment[i].id + '">'
							+ environment[i].title + '</option>';

				}
				contenthtml += "</optgroup>";
				// alert(contenthtml);
				$("#myselect").append(contenthtml);

				contenthtml = '<optgroup label="市容秩序">';
				for (var i = 0; i < cityOrder.length; i++) {
					contenthtml += '<option value="' + cityOrder[i].id + '">'
							+ cityOrder[i].title + '</option>';
				}
				contenthtml += "</optgroup>";
				$("#myselect").append(contenthtml);

				contenthtml = '<optgroup label="绿化美化">';
				for (var i = 0; i < greenCondition.length; i++) {
					contenthtml += '<option value="' + greenCondition[i].id
							+ '">' + greenCondition[i].title + '</option>';
				}
				contenthtml += "</optgroup>";
				$("#myselect").append(contenthtml);
				$("#myselect").selectmenu("refresh", true);

			}
		}
	});
};

function saveOrUpdateInspectInfo() {

	var bussinessId = $("#identify").val();
	var optionId = $("#optionId").val();
	//var needCheck = $("#needCheck").val();
	var remark = $("#remark").val();

	var enviromentIds = $("#myselect").val();
	alert(enviromentIds);
	if (enviromentIds == ''||enviromentIds==null||enviromentIds==undefined) {
		alert('请选择巡检选项!');
		return false;
	}
	
	var param = {
		"bussinessId" : bussinessId,
		"optionId" : optionId,
	//	"needCheck" : needCheck,
		"remark" : remark,
		"enviromentIds" : enviromentIds
	};

	$.ajaxFileUpload({
		url : window.rootPath + "/ui/bussinessInspection/saveOrUpdate",
		secureuri : false,// 是否需要安全协议
		fileElementId : 'file_upload',// 文件上传的Id属性
		dataType : 'JSON',
		data : param,
		success : function(result, status) {
			window.location.href = window.rootPath
					+ "/ui/bussinessInspection?device=mobile&identify="
					+ bussinessId;
		}
	});
	$("#popupView").popup("open");
	// 设置按钮无法点击
	// $("#submitBtn").prop("disabled", true);
	return false;
};